<?php
$conn_args = array(
	'host' => '192.168.110.130',
	'port' => '5672',
	'login' => 'root',
	'password' => 'root',
	'vhost'=>'/'
);

$exchange_name = 'topic_exchange';	//交换机名
// $queue_name = 'topic_queue';		//队列名（topic不需要队列名）
$routing_key = 'com';
// $routing_key = 'com.aa';
// $routing_key = 'com.aa.bb';
if($_GET['r_key']){
	$routing_key = $_GET['r_key'];
}


//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
	die("连接RabbitMQ失败！");
}
$channel = new AMQPChannel($conn);

//创建交换机
$exchange = new AMQPExchange($channel);
$exchange->setName($exchange_name);
$exchange->setType(AMQP_EX_TYPE_TOPIC); //topic类型
$exchange->setFlags(AMQP_DURABLE); //持久化
$exchange->declareExchange();	//创建操作

//创建队列（topic不需要队列，也不需要绑定）
// $queue = new AMQPQueue($channel);
// $queue->setName($queue_name);
// $queue->setFlags(AMQP_DURABLE);	//持久化
// $queue->declareQueue();	//最好队列object在这里declare()下，否则如果是新的queue会报错

//绑定交换机与队列，并指定路由键，可以多个路由键
// $queue->bind($exchange_name, $routing_key);

for($i=1; $i <= 3; $i++){
	$msg = $i . "、消息测试：【 " . $routing_key. " 】" . date('Y-m-d H:i:s', time());
	$re = $exchange->publish($msg, $routing_key);
}
echo "发送了 " . ($i - 1) . " 条消息。【 " . $routing_key. " 】";
//echo "Send Message:".$exchange->publish("rabbitmq消息测试，key_2 by xust" . date('H:i:s', time()), 'key_2').PHP_EOL;









?>

